//
//  FillLogistViewController.m
//  yigou
//
//  Created by 秦乾龙 on 17/2/24.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "FillLogistViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "User.h"
#import "ZYTableViewCell.h"
#import "ChooseLogistCompanyViewController.h"
#import "ZYTextField.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface FillLogistViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,ChooseLogistCompanyDelegate>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) NavView *nav;
@property (nonatomic,retain) NSArray *titleImageNameArr;
@property (nonatomic,retain) NSArray *titleArr;
@property(nonatomic,retain)User *user;
@property(nonatomic,retain)UITextField *logistNummerField;
@end

@implementation FillLogistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"填写物流"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    UIButton *buttonNotice = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNotice.frame = CGRectMake(0, 0, 80, 20);
    buttonNotice.titleLabel.font = [UIFont systemFontOfSize:15];
    [buttonNotice setTitle:@"确定发货" forState:UIControlStateNormal];
    
    UIBarButtonItem *btnNoticeItem = [[UIBarButtonItem alloc]initWithCustomView:buttonNotice];
    [buttonNotice addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = btnNoticeItem;
    
    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenW , kScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
//    _companyStr = @"选择物流公司";
    
}
- (void)returnName:(NSString *)name
{
    self.companyStr = name;
    [_mainTableView reloadData];
}
- (void)returnId:(NSString *)companyId
{
    self.companyId = companyId;
//    [_mainTableView reloadData];
}
- (void)request
{
    NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/getUserById",APIYIGOU]
    ;
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"111%@",allDic);
        _user = [[User alloc]initWithDic:allDic[@"data"]];
        [_mainTableView reloadData];
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    NSArray *titleArr = [[NSArray alloc]initWithObjects:@"物流公司",@"物流单号",nil];
    UILabel *titleLable = [[UILabel alloc]init];
    titleLable.font = [UIFont systemFontOfSize:15];
    titleLable.text = titleArr[indexPath.row];
    CGSize titleSize = [titleLable sizeThatFits:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    titleLable.frame = CGRectMake(30*kScale, (100-44)*kScale/2, titleSize.width, 44*kScale);
    UILabel *contentLable = [[UILabel alloc]init];
    contentLable.font = [UIFont systemFontOfSize:15];
//    CGFloat leftSpace = 30*kScale;
    if (indexPath.row == 0)
    {
//        contentLable.text = self.companyStr;
        if (_companyStr) {
            contentLable.text = _companyStr;
        }
        else
        {
            contentLable.text = @"选择物流公司";
        }
        contentLable.textColor = ZallColorFromRGB(0x9e9e9e);
        //右侧小箭头
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    else if (indexPath.row==1)
    {
        _logistNummerField = [[UITextField alloc]init];
        _logistNummerField.frame = CGRectMake(60*kScale+titleSize.width, 0, kScreenW-90*kScale-60*kScale-titleSize.width, 100*kScale);;
//        _logistNummerField.textColor = [UIColor whiteColor];
        
        _logistNummerField.placeholder = @"请填写运单号";
        _logistNummerField.font = [UIFont systemFontOfSize:15];
        _logistNummerField.delegate =self;
        _logistNummerField.tag = 11;
//        _logistNummerField.secureTextEntry = YES;
        _logistNummerField.textAlignment = NSTextAlignmentLeft;
        _logistNummerField.keyboardType = UIKeyboardTypeASCIICapable;
        [cell addSubview:_logistNummerField];
//        contentLable.text = @"请填写运单号";
//        contentLable.textColor = ZallColorFromRGB(0x9e9e9e);
    }
    
   
    contentLable.frame = CGRectMake(60*kScale+titleSize.width, 0, kScreenW-90*kScale-60*kScale-titleSize.width, 100*kScale);
    contentLable.textAlignment = NSTextAlignmentLeft;
    [cell addSubview:titleLable];
    
    [cell addSubview:contentLable];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 20*kScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        ChooseLogistCompanyViewController *vc = [[ChooseLogistCompanyViewController alloc]init];
        vc.delegate = self;
        vc.companyStr = _companyStr;
        [self.navigationController pushViewController:vc animated:YES];
    }
   
    
}

- (void)backClick
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void) sureClick:(UIButton*)sender
{
    if (!_companyStr) {
        [ViewFactory showInfoMsg:@"请选择物流公司"];
        return;
    }
    if (_logistNummerField.text.length==0) {
        [ViewFactory showInfoMsg:@"请输入物流单号"];
        return;
    }
    NSString *urlStr = [NSString stringWithFormat:@"%@/order/orderFh?order_id=%@&kuaidi_id=%@&kuaidi_order_id=%@",APIYIGOU,_orderId,_companyId,_logistNummerField.text]
    ;
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"111%@",allDic);
//        _user = [[User alloc]initWithDic:allDic[@"data"]];
        [ViewFactory showInfoMsg:@"发货成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } error:^(YTKBaseRequest *request) {
        [ViewFactory showInfoMsg:@"发货失败"];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];

    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self request];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
